const {net} = require('electron')
const querystring = require('querystring')
const Common = require('./constants').default

export function getBalance(publicKey,callback){
    var querys = querystring.stringify({publicKey:publicKey})
    var options ={
        url:Common.GET_BALANCE_URL+'?'+querys,
    }
    var resp_obj = null
    // console.info(Common.GET_BALANCE_URL)
    var request = net.request(options,function(response){
        response.setEncoding('utf8');
        // console.info(response.statusCode)
        var result = ''
        response.on('data',(data)=>{
            result = result + data;
        })
        response.on('end', (data) => {
            // console.info(`body:${result}`)
            if(response.statusCode==200){
                var str = result.toString()
                resp_obj = JSON.parse(str)
                // console.log(response)
                return callback(resp_obj)
            } else {
                return callback({success:false})
            }     
        })
    })
    request.end();
  }; 


export function getAccountInfo(key,page,size,callback){
    var querys = querystring.stringify({key:key,page:page,size:size})
    var options ={
        url:Common.DLB_EXPLORER_URL+'account'+'?'+querys,
    }
    var resp_obj = null
    var request = net.request(options,function(response){
        response.setEncoding('utf8');
        var result = ''
        response.on('data',(data)=>{
            result = result + data;
        })
        response.on('end', (data) => {
            // console.info(`body:${result}`)
            if(!response.statusCode==200){
                return callback({code:0,msg:result})
            }
            try{
                var str = result.toString()
                resp_obj = JSON.parse(str)
            } catch(err){
                return callback({code:0,msg:err})
            }              
            return callback({code:200,msg:resp_obj})              
        })
    })
    request.end();
};

export function getTransactionInfo(hash,callback){
    var querys = querystring.stringify({hash:hash})
    var options ={
        url:Common.DLB_EXPLORER_URL+'transaction'+'?'+querys,
    }
    var resp_obj = null
    var request = net.request(options,function(response){
        response.setEncoding('utf8');
        var result = ''
        response.on('data',(data)=>{
            result = result + data;
        })
        response.on('end', (data) => {
            // console.info(`body:${result}`)
            if(!response.statusCode==200){
                return callback({code:0,msg:result})
            }
            try{
                var str = result.toString()
                resp_obj = JSON.parse(str)
            }catch(err){
                return callback({code:0,msg:err})
            }
            return callback({code:200,msg:resp_obj})
            
        })
    })
    request.end();
};

export function transfer(sendPublic,secret,recipient,amount,callback){
    var content = JSON.stringify({secret: secret, publicKey: sendPublic, amount: ~~amount, recipientPublicKey: recipient});
    // console.info(content)
    var options = {
        hostname: 'www.dlbchain.natapp1.cc',		//'127.0.0.1',
        method:'POST',
        path:'/api/transactions/',
        // port:3091,
        headers:{
            'Content-Type':'application/json',
            // 'Content-Type':'application/x-www-form-urlencoded',
            'Content-Length':content.length
        }
    }
    console.log(content)
    var request = net.request(options,function(response){
        // console.log('STATUS:'+response.statusCode);
        // console.log('HEADERS:'+JSON.stringify(response.headers));
        response.setEncoding('utf8');
        var result = ''
        response.on('data',(data)=>{
            result = result + data;
        })
        response.on('end', (data) => {
            // console.info(`body:${result}`)
            try{
                var str = result.toString()
                var obj = JSON.parse(str)
            } catch(err){
                return callback({code:0,msg:err})
            }
            console.log(obj)
            return callback(obj)
        })
    })
    request.write(content);                
    request.end();
}

export function getInitdata(callback){
    // console.log(options)
    var options ={
        url:Common.DLB_EXPLORER_URL+'initdata',
    }
    // console.log(options)
    var request = net.request(options,function(response){
        // console.log('STATUS:'+response.statusCode);
        // console.log('HEADERS:'+JSON.stringify(response.headers));
        response.setEncoding('utf8');
        var result = ''
        response.on('data',(data)=>{
            result = result + data;
        })
        response.on('end', (data) => {
            // console.info(`body:${result}`)
            try{
                var str = result.toString()
                var obj = JSON.parse(str)
            } catch(err){
                return callback({code:0,msg:err})
            }
            return callback({code:200,msg:obj})
        })
    })
    // request.write(content);                
    request.end();
}

export function createAccount(callback){
    const request = net.request(Common.CREATE_ACCOUNT_URL)
    request.on('response',(response)=>{
        // console.log(`status:${response.statusCode}`)
        // console.log(`HEADERS: ${JSON.stringify(response.headers)}`)
        var result = ''
        response.on('data',(data)=>{
            result = result + data;
        })
        response.on('end', (data) => {
            try{
            var str = result.toString()
            var obj = JSON.parse(str)
            }catch(err){
                return callback({code:0,msg:err})
            }
            return callback({code:200,msg:obj})

        })
    });
    request.end();
    // event.sender.send('show-account')
}